/**
 * 系统管理 岗位管理 rule
 */
<template>
  <div>
    <!-- 面包屑导航 type-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>岗位管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="searchForm" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="searchForm.no" placeholder="请输入岗位编号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-search" @click="searchPlus">高级搜索</el-button>
        <el-button size="small" type="success" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
        <el-button size="small" type="danger" @click="batchDelete">批量删除</el-button>
        <el-button size="small"  @click="resetPage()">展示所有组织</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="positionData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;"  @selection-change="handleSelectionChange" :row-key="getRowKeys" ref="multipleSelectable">
      <el-table-column align="center" type="selection" width="50"></el-table-column>
      <el-table-column align="center" sortable prop="id" label="岗位序号" width="100"></el-table-column>
      <el-table-column align="center" sortable prop="no" label="岗位编号" width="100"></el-table-column>
      <el-table-column align="center" sortable prop="name" label="岗位名称" width="120"></el-table-column>
      <el-table-column align="center" sortable prop="type" label="岗位类型" width="120"></el-table-column>
      <el-table-column align="center" sortable prop="responsibilities" label="岗位职责" min-width="120"></el-table-column>
      <el-table-column label="操作" min-width="400" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="80px" ref="editForm" :model="editForm" >
        <el-form-item label="岗位编号" prop="no">
          <el-input v-model="editForm.no" auto-complete="off" placeholder="请输入岗位编号" :disabled="isdisabled"></el-input>
        </el-form-item>
        <el-form-item label="岗位名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off" placeholder="请输入岗位名称"></el-input>
        </el-form-item>
        <el-form-item label="岗位类型" prop="type">
          <el-input v-model="editForm.type" auto-complete="off" placeholder="请输入岗位类型"></el-input>
        </el-form-item>
        <el-form-item label="岗位职责" prop="responsibilities">
          <el-input v-model="editForm.responsibilities" placeholder="请输入岗位职责"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click='closeDialog("edit")'>取消</el-button>
        <el-button type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
    <!-- 高级搜索 -->
    <el-dialog title="高级搜索" :visible.sync="searchVisible" width="38%" @click="closeDialog">
      <el-form ref="searchForm" :model="searchForm" label-width="80px" inline>
        <el-form-item label="岗位编码">
          <el-input v-model="searchForm.no" placeholder="请输入岗位编码" style="width:220px"></el-input>
        </el-form-item>
        <el-form-item label="岗位名称">
          <el-input v-model="searchForm.name" auto-complete="off" placeholder="请输入岗位名称" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="岗位类型">
          <el-input v-model="searchForm.type" placeholder="请输入岗位类型" style="width:220px"></el-input>
        </el-form-item>
        <el-form-item label="岗位职责">
          <el-input v-model="searchForm.responsibilities" placeholder="请输入岗位职责" style="width: 220px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button @click="resetCondition">重置</el-button>
        <el-button @click='closeDialog("高级搜索")'>取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入请求方法
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      loading: false, //是显示加载
      title: '添加用户',
      editFormVisible: false, //控制编辑页面显示与隐藏
      // 编辑与添加
      editForm: {
        no:'',
        name:'',
        type:'',
        responsibilities:'',
        token: localStorage.getItem('logintoken')
      },
      // 请求数据参数
      searchForm: {
        no:'',
        name:'',
        type:'',
        responsibilities:'',
      },
      //用户数据
      userData: [],
      //岗位数据
      positionData:[],
      //勾选组织ids
      multipleSelection: [],
      //控制高级搜索框的显示与隐藏
      searchVisible:false,
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
      //控制增加和修改页面“编码”禁用功能
      isdisabled:''
    }
  },
  // 注册组件
  components: {
    Pagination,
  },

  watch: {},

  created() {
    this.getPositionData()
  },
  methods: {
    //获取岗位数据
    getPositionData(){
      this.$axios.get('/api/position/'+this.pageparm.currentPage+'/'+this.pageparm.pageSize).then(res => {
      console.log(res);
      if(res.data.code == 200){
        this.positionData = res.data.data.list
        // 分页赋值
        this.pageparm.total = res.data.data.total
      }
      }).catch();
    },
    // 分页插件事件
    callFather(parm) {
      this.pageparm.currentPage = parm.currentPage
      this.pageparm.pageSize = parm.pageSize
      this.getdata()
    },
    // 搜索事件
    search() {
      this.$axios.post('/api/position/search',this.searchForm).then(res => {
        console.log(res);
        if(res.data.code == 200){
          this.positionData = res.data.data
        }
        this.pageparm.total = res.data.data.length
        this.closeDialog('高级搜索');
      }).catch(()=>{});
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.isdisabled = true
        this.title = '修改岗位'
        this.editForm.no = row.no
        this.editForm.name = row.name
        this.editForm.type = row.type
        this.editForm.responsibilities = row.responsibilities
      } else {
        this.isdisabled = false
        this.title = '添加岗位'
        this.editForm.no = ''
        this.editForm.name = ''
        this.editForm.type = ''
        this.editForm.responsibilities = ''
      }
    },
    // 编辑、增加页面保存方法
    submitForm() {
      if(this.title=='添加岗位'){
        this.$axios.post('/api/position/add',this.editForm).then(res => {
          console.log(res);
          if(res.data.code == 200){
            this.$message({
              type: 'success',
              message: '添加成功!'
            });
          }
          this.getPositionData();
          this.editFormVisible = false
        }).catch();
      }else{
        this.$axios.put('/api/position/update',this.editForm).then(res => {
          console.log(res);
          if(res.data.code == 200){
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
          }
          this.getPositionData();
          this.editFormVisible = false
        }).catch();
      }
    },
    handleClick(data, checked, node) {
      if (checked) {
        this.$refs.tree.setCheckedNodes([])
        this.$refs.tree.setCheckedNodes([data])
        this.unitparm.deptId = data.id
        this.unitparm.deptName = data.name
        //交叉点击节点
      } else {
      }
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == 'edit') {
        this.editFormVisible = false
      } else if (dialog == 'perm') {
        this.dataAccessshow = false
      } else if (dialog == 'unit') {
        this.unitAccessshow = false
      } else if (dialog == '高级搜索'){
        this.searchVisible = false
      }
    },
    //删除岗位
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          console.log(row.id);
          this.$axios.delete('/api/position/delete/'+row.id).then(res => {
            console.log(res);
            if(res.data.code == 200){
              this.$message({
                type: 'success',
                message: '删除成功'
              })
              this.getPositionData();
            }
          }).cathch(()=>{});
        })
      .catch(()=>{})
    },
    //高级搜索事件
    searchPlus(){
      //显示高级搜索框
      this.searchVisible = true;
      this.resetCondition();
    },
    resetCondition(){
      this.searchForm.no = ''
      this.searchForm.name = ''
      this.searchForm.type = ''
      this.searchForm.responsibilities = ''
    },
    //批量删除
    batchDelete(){
      console.log(this.multipleSelection);
      if(this.multipleSelection.length==0){
            this.$message({
                type: "info",
                message: "请勾选需要删除的列",
            });
            return;
        }
        this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        })
        .then(() => {
            this.$axios.put("/api/position/batchDelete",this.multipleSelection)
            .then((res) => {
              this.$refs.multipleSelectable.clearSelection();
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "已删除!",
                });
                this.getPositionData();
              } else {
                this.$message({
                  type: "info",
                  message: res.data.message,
                });
              }
            })
            .catch((err) => {
              this.$message.error("删除失败，请稍后再试！");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //获取被勾选的组织的id
    handleSelectionChange(organizations) {
      for(let index in organizations){
        this.multipleSelection[index] = organizations[index].id;
      }
      console.log(this.multipleSelection);
    },
    getRowKeys(row) {
      return row.id;
    },
    resetPage(){
      this.pageparm.currentPage = 1
      this.pageparm.pageSize = 10
      this.getPositionData();
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 